<script type="text/javascript">

    function createContactPanel(){
        var cPanel;
        if (!cPanel) {
            cForm = Ext.create('Ext.form.Panel', {
                action:'submit',
                defaults:{
                    anchor:'100%'
                },
                bodyPadding:10,
                items:[

                    {
                        xtype:'combobox',
                        fieldLabel:'Contact',
                        store:contactsStore,
                        queryMode:'local',
                        displayField:'value',
                        valueField:'key',
                        name:'contact',
                        grow: true
                    }

                ]
            });
            cPanel = Ext.create('Ext.window.Window', {
                title:'Add contact',
                width:500,
                height:125,
                layout:'fit',
                items:cForm,
                closeAction:'hide',
                buttons:[
                    {
                        text:'Add',
                        handler:function () {
                            var mailValue = mailForm.getForm().findField('email').getValue();
                            if(mailValue=='')
                                mailForm.getForm().findField('email').setValue(cForm.getForm().findField('contact').getValue());
                            else{
                                mailForm.getForm().findField('email').setValue(mailValue + ', '+cForm.getForm().findField('contact').getValue());
                            }

                            cPanel.hide();

                        }
                    },
                    {
                        text:'Close',
                        handler:function () {

                            cPanel.hide();
                        }
                    }
                ]
            });
        }
        cPanel.show();
        cPanel.center();
    }
    function getPDF() {
        var contractType = $('#contractType').val();
        var url = 'index.php?r=pdf/ContractReport&custno=<?=$custno?>&type=' + contractType;
        window.open(url);
    }
    function createMailPanel() {
        var mailPanel;
        if (!mailPanel) {
            var contractTypeText = $('#contractType :selected').text();
            var contractType = $('#contractType').val();
            console.log(contractTypeText);
            mailForm = Ext.create('Ext.form.Panel', {
                action:'submit',
                defaults:{
                    anchor:'100%'
                },
                bodyPadding:10,
                items:[
                    {
                        xtype:'box',
                        autoEl:{
                            tag:'a',
                            href:'javascript:void(0)',
                            onclick:'createContactPanel()',
                            html:'Add Contact '
                        }
                    },
                    {
                        fieldLabel:'Email',
                        xtype:'textfield',
                        name:'email',
                        allowBlank:false
                    },
                    {
                        fieldLabel:'Subject',
                        xtype:'textfield',
                        name:'subject',

                        allowBlank:false,
                        value:'Contract Report for contract type : ' +contractTypeText
                    },

                    {

                        xtype:'htmleditor',
                        enableColors:false,
                        enableAlignments:false,
                        name:'message',
                        fieldLabel:'Message',
                        anchor:'100%'
                    }
                ]
            });

            mailPanel = Ext.create('Ext.window.Window', {
                title:'Mail Form',
                width:600,
                height:400,
                layout:'fit',
                items:mailForm,
                closeAction:'hide',
                buttons:[
                    {
                        text:'Submit',
                        handler:function () {

					
									showOkDialog('PDF generation can take a while');
                            mailForm.submit({

                                url:'index.php?r=pdf/ContractReport&custno=<?=$custno?>&mail=1&type=' + contractType,

                                success:function (form, action) {

									showOkDialog(action.result.msg);
                                    mailPanel.close();
                                },
                                failure:function (form, action) {
                                    Ext.Msg.alert('Failed', action.result.msg);
                                }

                            });
                            mailPanel.hide();

                        }
                    },
                    {
                        text:'Close',
                        handler:function () {

                            mailPanel.hide();
                        }
                    }
                ]
            });
        }
        mailPanel.show();
        mailPanel.center();
    }


    Ext.onReady(function () {
        contactsStore = Ext.create('Ext.data.Store', {
            model:'Combomodel',
            data:contacts
        });


    });

</script>
<?php
$this->breadcrumbs = array(
    'Report',
);?>
<h1>Contract Report</h1>

<div class="form">

    <?php
    echo CHtml::form('', 'post', array('id' => 'reportForm'));
    ?>


    <div class="row">
        <?php echo CHtml::label('Contract Type', 'contractType'); ?>
        <?php echo CHtml::dropDownList('contractType','',ContractType::getListData()) ?>
    </div>


    <div class="row">
        <?php echo CHtml::link('Download in PDF', '#', array('id' => 'reportPDF',
        'onclick' => 'javascript:getPDF();')) ?>
        <?php echo CHtml::link('Send to Email', '#', array('id' => 'mailPdf',
        'onclick' => 'javascript:createMailPanel();')) ?>
    </div>
    <?
    echo CHtml::endForm();
    ?>

</div>
